package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.PageResult;
import com.heima.pojo.Node;
import com.heima.pojo.VendingMachine;
import com.heima.pojo.dto.NodeDto;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.RegionVo;
import com.heima.pojo.vo.VendingMachineVo;
import com.heima.service.INodeService;
import com.heima.service.IRegionService;
import com.heima.service.IVendingMachineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 点位表 前端控制器
 *
 * @author xjy
 * @since 2024-06-05
 */
@RestController
@RequestMapping("/vm-service/node")
@Api(tags = "点位管理")
public class NodeController {
    @Autowired
    private INodeService nodeService;
    @Autowired
    private IVendingMachineService vendingMachineService;

    @GetMapping("/search")
    @ApiOperation("分页查询点位列表")
    public PageResult<NodeVo> search(PageResult<NodeVo> pageResult, @ApiParam(value = "区域Id", required = false) Long regionId) {
        return nodeService.PageSearch(pageResult, regionId);
    }

    @PostMapping
    @ApiOperation("添加点位")
    public Boolean addNode(@RequestBody NodeDto nodeDto) {
        if (ObjectUtil.isEmpty(nodeDto)) {
            return false;
        }
        nodeDto.setCreateTime(LocalDateTime.now());
        nodeDto.setUpdateTime(LocalDateTime.now());
        return nodeService.save(BeanUtil.toBean(nodeDto, Node.class));
    }

    @GetMapping("/vmList/{nodeId}")
    @ApiOperation("获取点位详情信息")
    public List<VendingMachineVo> nodeDetail(@PathVariable @ApiParam(value = "点位id", required = true) Long nodeId) {
        LambdaQueryWrapper<VendingMachine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VendingMachine::getNodeId, nodeId);
        return BeanUtil.copyToList(vendingMachineService.list(wrapper), VendingMachineVo.class);
    }

    @PutMapping("/{nodeId}")
    @ApiOperation("修改点位信息")
    public Boolean updateNode(@PathVariable @ApiParam(value = "点位id", required = true) Long nodeId,
                              @RequestBody @ApiParam(value = "点位信息", required = true) NodeDto nodeDto) {
        if (ObjectUtil.isEmpty(nodeDto) || ObjectUtil.isEmpty(nodeId)) {
            return false;
        }
        Node node = BeanUtil.toBean(nodeDto, Node.class);
        node.setId(nodeId);
        node.setUpdateTime(LocalDateTime.now());
        return nodeService.updateById(node);
    }

    @DeleteMapping("/{nodeId}")
    @ApiOperation("删除点位")
    public Boolean deleteNode(@PathVariable @ApiParam(value = "点位id", required = true) Long nodeId) {
        if (ObjectUtil.isEmpty(nodeId)) {
            return false;
        }
        LambdaQueryWrapper<VendingMachine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VendingMachine::getNodeId, nodeId);
        int count = vendingMachineService.count(wrapper);
        if (count > 0) {
            return false;
        }
        return nodeService.removeById(nodeId);
    }

    @GetMapping("/nodeCollect")
    @ApiOperation("合作商点位汇总统计")
    public List<Map<String, Object>> nodeCollect() {
        return nodeService.nodeCollect();
    }

    @GetMapping("/count")
    @ApiOperation("获取点位总数量")
    public Integer count() {
        return nodeService.count();
    }
}
